package cn.lbwfree.day3_24;

import java.util.Scanner;

/**
 * @date:2020年3月24日
 * @author:lbwfree
 * @number:BASIC-17
 */
public class MatrixPower {
	public static void main(String[] args) {
		Scanner inScanner = new Scanner(System.in);
		int n=inScanner.nextInt();
		int power = inScanner.nextInt();
		if(power<0||power>10)
		{
			return;
		}
		if(power==0)
		{
			for(int i=0;i<n;i++)
			{
				for(int j =0; j<n; j++)
				{
					if (i!=j) {
						System.out.print(0+" ");
					}else {
						System.out.print(1+" ");
					}
				}
				System.out.println();
			}
			return;
		}
		int[][] matrix = new int[n][n];
		int[][] matrix1 = new int[n][n];
		for(int i=0;i<n;i++)
		{
			for(int j =0; j<n; j++)
			{
				matrix[i][j]=inScanner.nextInt();
				matrix1[i][j]=matrix[i][j];
			}
		}
		if(power==1)
		{
			for(int i=0;i<n;i++)
			{
				for(int j =0; j<n; j++)
				{
					System.out.print(matrix[i][j]+" ");
				}
				System.out.println();
			}
			return;
		}
		int[][] temp=new int[n][n];;
		for (int i = 0; i < power-1; i++) { //次幂的次数
			temp=new int[n][n];
			
			//3次循环求矩阵
			for (int j = 0; j < n; j++) {
				for (int j2 = 0; j2 < n; j2++) {
					for (int k = 0; k < n; k++) {
						temp[j][j2] += matrix1[j][k]*matrix[k][j2];
					}
				}
			}
			matrix1=temp;
		}
		for(int i=0;i<n;i++)
		{
			for(int j =0; j<n; j++)
			{
				System.out.print(temp[i][j]+" ");
			}
			System.out.println();
		}
	}
}
